<?php

class controller_user_access extends controller
{

    // 登陆页面
    public function login()
    {
        $my = bone::get_user();
        if ($my->id>0) $this->redirect( BONE_URL );

        $template = $this->get_template();
        $template->set_title('用户登陆');
        $template->display();
    }

    // 登陆检查
    public function login_check()
    {
        $username = post('username', '');
        $password = post('password', '');
        $rememberme = post('rememberme', '0');
        
        $return = post('return', '');
        
        if (!$username || !$password) $this->redirect(url('/?action=user.access.login&return=' . $return), '用户名或密码不能为空', 'error');
        
        $model = $this->get_model();
        if ($model->login($username, $password, $rememberme))
            $this->redirect($return == '' ? url('/') : base64_decode($return));
        else
            $this->redirect(url('/?action=user.access.login&return=' . $return), $model->get_error(), 'error');
    }

    // 登陆检查
    public function ajax_login_check()
    {
        $username = post('username', '');
        $password = post('password', '');
        $rememberme = post('rememberme', '0');
        
        if (!$username || !$password)
        {
            $this->set_ajax('status', 1);
            $this->set_ajax('description', '用户名或密码不能为空');
            $this->ajax();
        }
        
        $model = $this->get_model();
        if ($model->login($username, $password, $rememberme))
        {
            $this->set_ajax('status', 0);
            $this->set_ajax('description', '登陆成功');
            $this->ajax();
        }
        else
        {
            $this->set_ajax('status', 3);
            $this->set_ajax('description', $model->get_error());
            $this->ajax();
        }
    }

    // 注册新用户
    public function register()
    {
        $template = $this->get_template();
        $template->set_title('注册新用户');
        $template->display();
    }

    // 保存新注册用户
    public function ajax_register_save()
    {
        $username = post('username', '');
        $email = post('email', '');
        $name = post('name', '');
        $password = post('password', '');
        $confirm_password = post('confirm_password', '');
        
        if ($username == '')
        {
            $this->set_ajax('status', 1);
            $this->set_ajax('description', '用户名不能为空');
            $this->ajax();
        }
        
        if ($email == '')
        {
            $this->set_ajax('status', 2);
            $this->set_ajax('description', '邮箱不能为空');
            $this->ajax();
        }
        
        if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i", $email))
        {
            $this->set_ajax('status', 3);
            $this->set_ajax('description', '邮箱格式非法');
            $this->ajax();
        }
        
        if ($password == '')
        {
            $this->set_ajax('status', 4);
            $this->set_ajax('description', '密码不能为空');
            $this->ajax();
        }
        
        if ($password != $confirm_password)
        {
            $this->set_ajax('status', 5);
            $this->set_ajax('description', '两次输入的密码不一致');
            $this->ajax();
        }
        
        $model = $this->get_model();
        if ($model->register($username, $email, $password, $name))
        {
            $description = '您成功注册了以下账号: ';
            $description .= '<ul>';
            $description .= '<li> 用户名: ' . $username . '</li>';
            $description .= '<li> 邮箱: ' . $email . '</li>';
            $description .= '<li> 名字: ' . $name . '</li>';
            $description .= '</ul>';
            $description .= '<p>点击这里 <a href="' . url('./?action=user.access.login') . '">登陆</a></p>';
            
            $this->set_ajax('status', 0);
            $this->set_ajax('description', $description);
            $this->ajax();
        }
        else
        {
            $this->set_ajax('status', 6);
            $this->set_ajax('description', $model->get_error());
            $this->ajax();
        }
    }

    //找回密码表单
    public function forget_password()
    {
        $template = $this->get_template();
        $template->set_title('找回密码');
        $template->display();
    }

    //提交找回密码
    public function ajax_forget_password_save()
    {
        $type = post('type', '');
        $value = post('value', '');
        
        if (($type != 'username' && $type != 'email') || $value == '')
        {
            $this->set_ajax('status', 1);
            $this->set_ajax('description', '参数缺失, 请和管理员联系');
            $this->ajax();
        }
        
        $model = $this->get_model();
        if ($model->forget_password($type, $value))
        {
            $description = '系统已发送了一封重设密码的邮件到您注册时使用的邮箱中， 请查收。';
            
            $this->set_ajax('status', 0);
            $this->set_ajax('description', $description);
            $this->ajax();
        }
        else
        {
            $this->set_ajax('status', 2);
            $this->set_ajax('description', $model->get_error());
            $this->ajax();
        }
    }

    // 重设密码
    public function forget_password_reset()
    {
        $user_id = get('user_id', 0, 'int');
        $key = get('key', '');
        if ($user_id == 0 || $key == '') exit();
        
        $user = $this->get_table('user');
        $user->load($user_id);
        
        if ($user->key != $key) exit('该链接已失效');
        
        $template = $this->get_template();
        $template->set_title('重设密码');
        $template->set('user', $user);
        $template->display();
    }

    // 重设密码保存
    public function ajax_forget_password_reset_save()
    {
        $user_id = post('user_id', 0, 'int');
        $key = post('key', '');
        
        if ($user_id == 0 || $key == '')
        {
            $this->set_ajax('status', 1);
            $this->set_ajax('description', '参数缺失, 请和管理员联系');
            $this->ajax();
        }
        
        $password = post('password', '');
        $confirm_password = post('confirm_password', '');
        
        if ($password != $confirm_password)
        {
            $this->set_ajax('status', 2);
            $this->set_ajax('description', '两次输入的密码不一致');
            $this->ajax();
        }
        
        $model = $this->get_model();
        if ($model->forget_password_reset($user_id, $key, $password))
        {
            $description = '重设密码成功， 现在您可以使用新密码 <a href="' . url('./?action=user.access.login') . '">登陆</a> 了。';
            $this->set_ajax('status', 0);
            $this->set_ajax('description', $description);
            $this->ajax();
        }
        else
        {
            $this->set_ajax('status', 3);
            $this->set_ajax('description', $model->get_error());
            $this->ajax();
        }
    
    }

    // 退出登陆
    public function logout()
    {
        $model = $this->get_model();
        $model->logout();
        
        $this->redirect( url('/?action=user.access.login'), '成功退出');
    }

}

?>